package com.tengju.data.domain.model.report;

import com.tengju.data.domain.shared.CastUtil;
import com.tengju.data.domain.shared.DomainException;
import com.tengju.data.domain.shared.DomainExceptionCode;
import com.tengju.data.domain.shared.Month;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.util.Objects;

@Getter
@AllArgsConstructor
public class AchievementBonusDateConfig {


    private String dataKey;
    private LocalDate startDate;
    private LocalDate endDate;

    /**
     * 每个周期最终计算日期
     */
    private LocalDate lastCalDate;

    /**
     * 入账日期
     */
    private LocalDate settleDate;

    /**
     * 风控计算日期
     */
    private LocalDate riskDate;

    public boolean canLastCal(LocalDate date) {
        if (date == null || lastCalDate == null || endDate == null) {
            return false;
        }
//        if (!date.equals(lastCalDate)) {
//            return false;
//        }
        return date.isAfter(endDate);
    }

    public boolean canSettle(LocalDate date) {
        if (date == null || settleDate == null || endDate == null || lastCalDate == null) {
            return false;
        }
//        if (!date.equals(settleDate)) {
//            return false;
//        }
        return date.isAfter(endDate) && date.isAfter(lastCalDate);
    }

    /**
     * 是否周期内最后一天
     * 判断是否可以生成周期快照
     * @param date
     * @return
     */
    public boolean isPeriodEndDate(LocalDate date) {
        if (date == null || endDate == null) {
            return false;
        }
        return endDate.equals(date);
    }

    public boolean canCalRisk(LocalDate date) {
        if (date == null || settleDate == null || endDate == null || lastCalDate == null) {
            return false;
        }
//        if (!date.equals(riskDate)) {
//            return false;
//        }
        return date.isAfter(endDate) && date.isAfter(lastCalDate);
    }

    public Integer getYear() {
        return CastUtil.castInt(dataKey) / 100;
    }

    public Integer getMonthNum() {
        return CastUtil.castInt(dataKey) % 100;
    }

    /**
     * 默认情况配置
     * @param randomDate
     */
    public AchievementBonusDateConfig(LocalDate randomDate) {
        Month month = new Month(randomDate);
        this.dataKey = month.getMonthInt() + "";
        setDefaultDateRange(month);
        setDefaultLastCalDate(month);
        setDefaultSettleCalDate(month);
        setDefaultRiskDate(month);
    }

    public void setDefaultRiskDate(Month month) {
        if (StringUtils.isBlank(dataKey)) {
            throw new DomainException(DomainExceptionCode.MODEL_NOT_KEY);
        }
        this.riskDate = month.getEndDate().plusDays(2);
    }

    public void setDefaultDateRange(Month month) {
        if (StringUtils.isBlank(dataKey)) {
            throw new DomainException(DomainExceptionCode.MODEL_NOT_KEY);
        }
        this.startDate = month.getStartDate();
        this.endDate = month.getEndDate();
    }

    public void setDefaultLastCalDate(Month month) {
        if (StringUtils.isBlank(dataKey)) {
            throw new DomainException(DomainExceptionCode.MODEL_NOT_KEY);
        }
        this.lastCalDate = month.getEndDate().plusDays(1);
    }

    public void setDefaultSettleCalDate(Month month) {
        if (StringUtils.isBlank(dataKey)) {
            throw new DomainException(DomainExceptionCode.MODEL_NOT_KEY);
        }
        this.settleDate = month.getEndDate().plusDays(8);
    }

    @Override
    public String toString() {
        return "AchievementBonusDateConfig{" +
                "dataKey='" + dataKey + '\'' +
                ", startDate=" + startDate +
                ", endDate=" + endDate +
                ", lastCalDate=" + lastCalDate +
                ", settleDate=" + settleDate +
                ", riskDate=" + riskDate +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AchievementBonusDateConfig that = (AchievementBonusDateConfig) o;
        return Objects.equals(dataKey, that.dataKey) && Objects.equals(startDate, that.startDate) && Objects.equals(endDate, that.endDate) && Objects.equals(lastCalDate, that.lastCalDate) && Objects.equals(settleDate, that.settleDate) && Objects.equals(riskDate, that.riskDate);
    }

    @Override
    public int hashCode() {
        return Objects.hash(dataKey, startDate, endDate, lastCalDate, settleDate);
    }

    public boolean isComplete() {
        return StringUtils.isNotBlank(dataKey) &&
                startDate != null &&
                endDate != null &&
                lastCalDate != null &&
                settleDate != null &&
                riskDate != null;
    }

    public boolean isValid() {
        return endDate.isAfter(startDate) &&
                lastCalDate.isAfter(endDate) &&
                settleDate.isAfter(lastCalDate) &&
                riskDate.isAfter(lastCalDate);
    }
}
